﻿// License: https://opensource.org/licenses/MIT
// Create Date : 2018-01-01

using System;

namespace Cat.Extensions
{
    internal static partial class StringEx
    {
        /// <summary>
        /// 将对象转换为显示用的字符串
        /// </summary>
        /// <returns></returns>
        public static string ToDisplayString(this object obj)
        {
            if (obj == null) return "<null>";
            var message = obj.ToString();
            if (message == null) return "<" + obj.GetType().FullName + ">";
            if (message.Length == 0)
            {
                //obj is string
                if (Object.ReferenceEquals(message, obj)) return "<Empty>";
                return "<" + obj.GetType().FullName + ">";
            }
            return message;
        }
        /// <summary>
        /// 将对象转换为显示用的字符串
        /// </summary>
        /// <returns></returns>
        public static string SafeToDisplayString(this object obj)
        {
            try
            {
                return ToDisplayString(obj);
            }
            catch (Exception exc)
            {
                try
                {
                    //如果发生异常，把异常内容组织成字符串
                    return String.Format("<Error in StringEx.ToDisplayString for {0}>", exc.Message);
                }
                catch (Exception exc2) { return exc2.Message; }
            }
        }
    }
}
